package com.samplecompany.gui;

import static com.samplecompany.gui.MashupContainer.GID;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;

public class TabbedPanelTab extends Panel {
	/** */
	private static final long serialVersionUID = -6602527646776083146L;
	private final Link<Void> link;
	private final ITabbedPanelTarget tabbedPanelTarget;
	private final Class<? extends Panel> panelClass;

	/**
	 * @param tabbedPanelTarget
	 * @param title 
	 */
	public TabbedPanelTab(final ITabbedPanelTarget tabbedPanelTarget, Model<String> title, final Class<? extends Panel> panelClass) {
		super(GID);
		this.tabbedPanelTarget = tabbedPanelTarget;
		this.panelClass = panelClass;
		add(link = new Link<Void>("tab-link") {
			@Override
			public void onClick() {
				select();
			}
			
			@Override
			public boolean isEnabled() {
				return !(tabbedPanelTarget.getSelectedTab() == TabbedPanelTab.this);
			}
		});
		link.add(new Label("title", title));
	}

	/**
	 */
	public void select() {
		try {
			tabbedPanelTarget.setSelectedTabAndPanel(TabbedPanelTab.this, panelClass.getConstructor(String.class).newInstance(GID));
		} catch (Exception e) {
			throw new IllegalStateException(e);
		}
	}
}
